import axios from "@/utils/http";
import { ro } from "element-plus/es/locale";
//数据
const state = {
  //数据定义处
  historyData:[],
  patientInfo:null,
  patientBankRef:null,
  drugList:[],
};
function PrefixInteger(num:any, length:any) {
  return (Array(length).join('0') + num).slice(-length);
 }
 function getRandomStr(length:any){
  let num = PrefixInteger(Math.floor(Math.random()*1000000),length)
  return Number(new Date().toLocaleDateString().replaceAll("/","")+num)
 }
const getters = {
  getCode(state:any){
    let codeC = state.code
    state.code=0;
    return codeC;
  },
  //修饰后的数据获取方法
  getHistoryData(state:any){
    return state.historyData
  },
  getPatientInfo(state:any){
    let pi = {
      bankId: null,
      updateTime: null,
      bankName: "",
      sex: "男",
      age: 0,
      phone: null,
      idNumber: null,
      address: "",
      birthday: null,
      personalBankId: 1,
      allergicHistory: "",
      patient: null,
      recordsList: []
          }
    if(state.patientInfo){
    pi= state.patientInfo.patientBankRef
  }
  return pi
  },
  getPatient(state:any){
    if(state.patientInfo){
      return state.patientInfo.patientBankRef.patient
    }
  },
  getRecordsList(state:any){
    if(state.patientInfo){
      return state.patientInfo.patientBankRef.recordsList
    }
  },
  getRecordsRef(state:any){
    let str = getRandomStr(8)
    let ref = {
      "recordsId": str,
      "recordsTime": null,
      "recordType": null,
      "infect": null,
      "message": null,
      "mainSuit": null,
      "docterSuggest": null,
      "hospital": null,
      "personalRecordsId": null,
      "remarks": null,
      "seeDoctor": null,
      "rxs": []
  }
    console.log(state.patientInfo);
    if(state.patientInfo){
      
      ref = state.patientInfo.recordsRef
    }
    return ref;
  },
  getRxs(state:any){
    let str = getRandomStr(8)
    let rxs = [
      {"rxName": "处方一",
      "recordsId": null,
      "drugNumber": 1,
      "drugName": "",
      "measure": 1,
      "price": 1,
      "amount": 1,
      "rxId": str,
      "rxType": "",
      "rxDay": 1,
      "rxDosage": 1,
      "rxTotalDay": 1,
      "rxTotalDosage": 1,
      "medicationRequirements": "",
      "remarks": "",}
    ];
    if(state.patientInfo){
      rxs = state.patientInfo.recordsRef.rxs;
      if(rxs.length<1){
        rxs=[
          {"rxName": "处方一",
          "recordsId": state.patientInfo.recordsRef.recordsId,
          "drugNumber": 0,
          "drugName": "",
          "measure": 0,
          "price": 0,
          "amount": 0,
          "rxId": str,
          "rxType": "",
          "rxDay": 1,
          "rxDosage": 1,
          "rxTotalDay": 1,
          "rxTotalDosage": 1,
          "medicationRequirements": "",
          "remarks": "",}
        ]
      }
    }
      let sortRxs = <any>[]
      let rxsName = new Set<String>();
      rxs.forEach((item:any) => {
        rxsName.add(item.rxName);
      });
      rxsName.forEach(rxsName2=>{
        let arr = rxs.filter(item=>item.rxName==rxsName2)
        let item=arr
        if(arr.length<=1&&arr[0].drugName==""){
          item=[]
        }
        arr[0].rxId = str
        sortRxs.push({
          rx:arr[0],
          item:item
        })
      })
      return sortRxs;
  },
  getDrugList(state:any){
    return state.drugList
  }
};

const mutations = {
  //修改数据处 ，可在这写axios请求拿数据
  
  searchHis(state:any){
    axios({
      url:"/seeDoctor/newVisits/history",
      method:"get",
      params:{
      }
      }).then((res:any)=>{
        console.log("未测试",res);
        state.historyData=res.data.data
     }).catch(err=>{
      console.log(err);
     })
  },
  setInfoById(state:any,row:any){
    axios({
      url:"/diagnose/queryScriptBydiaId",
      method:"get",
      params:{
        diagnoseId:row.id
      }
      }).then((res:any)=>{
        state.patientInfo=res.data.data
     }).catch(err=>{
      console.log(err);
     })
  },
  setDrugList(state:any,drugName:any){
    console.log("selectDrugAllListByName---查询所有药品");
    
    axios({
      url:"/seeDoctor/newVisits/selectDrugAllListByName",
      method:"get",
      params:{
        drugName:drugName
      }
      }).then((res:any)=>{
        console.log("测试",res);
        state.drugList=res.data.data
     }).catch(err=>{
      console.log(err);
     })
  },
  async createTreat(state:any,data:any){
    
    await axios({
      url:"/seeDoctor/newVisits/saveAll",
      method:"post",
      data:data
      }).then((res:any)=>{
        
        state.code = res.data.code;
     }).catch(err=>{
      console.log(err);
     })
  },
};

const actions = {};

export default {
  state,
  mutations,
  actions,
  getters,
};
